/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 * <p>
 * MpmProjectStakeholderController 项目相关方
 * <p>
 * 修改纪录 2021-08-19 版本：1.0 liuchenguang 创建。
 *
 * @version 版本：1.0
 * @author 作者：liuchenguang 创建日期：2021-08-19
 */

package com.topscomm.mpm.controller;

import com.topscomm.mpm.service.impl.MpmProjectService;
import com.topscomm.mpm.service.impl.MpmProjectStakeholderService;
import com.topscomm.pub.util.ConvertUtil;
import java.util.Arrays;
import java.util.Map;

import com.topscomm.pub.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.mpm.controller.auto.MpmProjectStakeholderControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: liuchenguang
 * @date: 2021-08-19
 * @description:项目相关方Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectStakeholder")
public class MpmProjectStakeholderController extends MpmProjectStakeholderControllerAuto implements
    IMpmProjectStakeholderController {

    @Autowired
    public MpmProjectStakeholderService mpmProjectStakeholderService;
	@Autowired
	public MpmProjectService mpmProjectService;

    /**
     * @param paraMap
     * @return
     * @author: liuchenguang
     * @date: 2021-08-19
     * @description:转换出查询SQL语句
     * @modify:
     */
    @Override
    protected String spellListSql(Map<String, Object> paraMap) {
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" 1=1 ");
        if (paraMap.containsKey("qry_user")) {
            String value = paraMap.get("qry_user").toString();
            if (!StringUtil.isEmpty(value)) {
                if (value.matches("^[0-9]*$") || value.matches(".*\\d+.*")) {
                    sql.appendSingle(" and usercode like '{0}%'", value);
                } else {
                    sql.appendSingle(" and username like '%{0}%'", value);
                }
            }
        }
        if (paraMap.containsKey("qry_stakeholdercategory")) {
            String value = paraMap.get("qry_stakeholdercategory").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and stakeholdercategory = '{0}' ", value);
            }
        }
        if (paraMap.containsKey("qry_stakeholderimpact")) {
            String value = paraMap.get("qry_stakeholderimpact").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and stakeholderimpact = '{0}' ", value);
            }
        }
        if (paraMap.containsKey("qry_managementstrategy")) {
            String value = paraMap.get("qry_managementstrategy").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and managementstrategy = '{0}' ", value);
            }
        }
        if (paraMap.containsKey("qry_projectcode")) {
            String value = paraMap.get("qry_projectcode").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and projectcode = '{0}' ", value);
            }
        } else if (paraMap.containsKey("qry_projectid")) {
            // 根据项目id查询
            String projectId = ConvertUtil.convertToString(paraMap.get("qry_projectid"));
            if (!StringUtil.isEmpty(projectId)) {
                Map<String, Object> projectCodeMap = mpmProjectService
                    .queryFieldsById(projectId, Arrays.asList("projectcode"));
                String projectCode = ConvertUtil.convertToString(projectCodeMap.get("projectcode"));
                if (!StringUtil.isEmpty(projectId)) {
                    sql.appendSingle(" and projectcode = '{0}' ", projectCode);
                }
            }
        }
        if (paraMap.containsKey("qry_enabled")) {
            String value = paraMap.get("qry_enabled").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and enabled = {0} ", value);
            }
        }
        return sql.toString();
    }

    /**
     * @param paraMap
     * @return
     * @author: liuchenguang
     * @date: 2021-08-25
     * @description:重写refList，去重重复人员
     * @modify:
     */
    @Override
    @GetMapping(value = "/refList")
    public ResponseResult<Object> refList(
        @RequestParam(required = false) Map<String, Object> paraMap) {
        try {
            String sqlWhere = this.spellListSql(paraMap);
            Page page = mpmProjectStakeholderService.listRevokeRepeat(paraMap, sqlWhere);
            return ResponseResult.ok(page);
        } catch (Exception ex) {
            return handleControllerException(ex);
        }
    }

    /**
     * @param paraMap
     * @return ResponseResult<Object>
     * @author: liuchenguang
     * @date: 2021-08-11
     * @description:重写获取相关方列表
     * @modify:
     */
    @Override
    @GetMapping(value = "/list")
    public ResponseResult<Object> list(
        @RequestParam(required = false) Map<String, Object> paraMap) {
        try {
            String sqlWhere = this.spellListSql(paraMap);
            Page page = mpmProjectStakeholderService.listRevokeSpace(paraMap, sqlWhere);
            return ResponseResult.ok(page);
        } catch (Exception ex) {
            return handleControllerException(ex);
        }
    }

    /**
     * @param paraMap
     * @return
     * @author: liuchenguang
     * @date: 2021-08-24
     * @description: 获取工作组分类的内部人员列表
     * @modify:
     */
    @GetMapping(value = "/groupList")
    public ResponseResult<Object> groupList(
        @RequestParam(required = false) Map<String, Object> paraMap) {
        try {
            Map<String, Object> resultMap = mpmProjectStakeholderService.getGroupList(paraMap);
            return ResponseResult.ok(resultMap);
        } catch (Exception ex) {
            return handleControllerException(ex);
        }
    }
}

